from parse import Parser
from utils import sys_logger
from model_manage import ModelManager
from threading import Thread


class Alloter:
    """
    alloter is responsible for managing the allocation of data to various models based on the parsed data from the parser.
    This class is designed to efficiently handle and distribute data to multiple models concurrently, 
    ensuring that each model receives the data it needs without unnecessary delays or resource contention.
    """

    def __init__(self, model_manage: ModelManager, parser: Parser):
        self.model_manage = model_manage
        self.parser = parser
        self.need_stop = False

    def allot_run(self):
        """
        分配数据到模型
        """
        while not self.need_stop:
            # 获取数据, 阻塞方式
            msg = self.parser.get_msgbean()

            if msg is None: 
                break

            table_num = msg.table_num
            # 返回的可能不止一个模型，可能多个模型都需要这个数据。
            models = self.model_manage.get_model(table_num=table_num)
            if models is None or table_num == 0:
                continue

            # 更新模型数据
            for model in models:
                model.update_input_data(msg)
            # map(lambda model: model.update_input_data(msg), models)

        sys_logger.info("Alloter run stopped.")

    def stop_run(self):
        """
        停止分配器
        """
        self.need_stop = True


if __name__ == '__main__':

    # test the alloter

    pass
